package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.mvp.ui.widget.ClipViewLayout;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 头像裁剪Activity
 *
 * @author xubo
 */
@Route(path = RouterHub.APP_CLIPIMAGEACTIVITY)
public class ClipImageActivity extends BaseActivity {
    private static final String TAG = "ClipImageActivity";
    @BindView(R.id.clipViewLayout)
    ClipViewLayout clipViewLayout;
    @Autowired(name = Constants.ROUTER_BOOLEAN)
    boolean isRect;
    @Autowired(name = Constants.ROUTER_DATA)
    Uri uri;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        if (getIntent().hasExtra(Constants.CLIP_TYPE)) {
            isRect = getIntent().getBooleanExtra(Constants.CLIP_TYPE, false);
        }
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        if (isRect) {
            return R.layout.activity_rect_clip_image;
        } else {
            return R.layout.activity_clip_image;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {
    }

    @Override
    protected void onResume() {
        super.onResume();
        //设置图片资源
        clipViewLayout.setImageSrc(uri);
    }

    @OnClick({R.id.btn_cancel, R.id.bt_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                finish();
                break;
            case R.id.bt_ok:
                generateUriAndReturn();
                break;
            default:
        }
    }

    /**
     * 生成Uri并且通过setResult返回给打开的activity
     */
    private void generateUriAndReturn() {
        //调用返回剪切图
        Bitmap zoomedCropBitmap;
        zoomedCropBitmap = clipViewLayout.clip();
        if (zoomedCropBitmap == null) {
            Log.e("android", "zoomedCropBitmap == null");
            return;
        }
        Uri mSaveUri = Uri.fromFile(new File(getCacheDir(), "cropped_" + System.currentTimeMillis() + ".jpg"));
        if (mSaveUri != null) {
            OutputStream outputStream = null;
            try {
                outputStream = getContentResolver().openOutputStream(mSaveUri);
                if (outputStream != null) {
                    zoomedCropBitmap.compress(Bitmap.CompressFormat.JPEG, 90, outputStream);
                }
            } catch (IOException ex) {
                Log.e("android", "Cannot open file: " + mSaveUri, ex);
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            Intent intent = new Intent();
            intent.setData(mSaveUri);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
}
